package com.yxq.springboot.dao.impl;
import org.springframework.stereotype.Repository;
import com.yxq.springboot.bean.SpringBootUser;
import com.yxq.springboot.dao.SpringBootUserDao;
import com.yxq.springboot.util.base.JdbcDaoImpl;
import com.yxq.springboot.util.base.Page;
import com.yxq.springboot.util.base.Sql;


/**
 * 
 * @author yxq
 *
 */
@Repository
public class SpringBootUserDaoImpl extends JdbcDaoImpl implements SpringBootUserDao {


	@Override
	public int insert(SpringBootUser springBootUser) {
		String sql = "insert into springboot_user (name, create_time) values (?, ?)";
		return jdbcTemplate.update(sql, springBootUser.getName(), springBootUser.getCreateTime());
	}

	@Override
	public int deleteById(int id) {
		String sql = "delete from springboot_user where id=?";
		return jdbcTemplate.update(sql, id);
	}

	@Override
	public int updateById(SpringBootUser springBootUser) {
		String sql = "update springboot_user set name=?, create_time=? where id=?";
		return jdbcTemplate.update(sql, springBootUser.getName(), springBootUser.getCreateTime(), springBootUser.getId());
	}

	@Override
	public SpringBootUser selectById(int id) {
		String sql = "select * from springboot_user where id=?";
		/*return jdbcTemplate.queryForObject(sql, new RowMapper<springBootUser>() {
			@Override
			public springBootUser mapRow(ResultSet rs, int rowNum) throws SQLException {
				springBootUser springBootUser = new springBootUser();
				springBootUser.setId(rs.getInt("id"));
				springBootUser.setName(rs.getString("name"));
				springBootUser.setCreateTime(rs.getDate("create_time"));
				return springBootUser;
			}
		}, id);*/
		
		return queryForObject(sql, SpringBootUser.class, id);
	}
	
	@Override
	public Page<SpringBootUser> queryForPage(int pageCurrent, int pageSize, String name){
		// 确定参数
		/*String sql = "select * from springboot_user where name=?";
		return queryForPage(sql.toString(), pageCurrent, pageSize, springBootUser.class, name);*/
		
		// 若name可能为空，则要进行判定，如下
		/*StringBuffer sql = new StringBuffer("select * from springboot_user where 1");
		if(!StringUtils.isNullOrEmpty(name)){
			// Sql.checkSql 的作用是防止sql注入
			sql.append(" and name = '").append(Sql.checkSql(name)).append("' ");
		}
		return queryForPage(sql.toString(), pageCurrent, pageSize, springBootUser.class);*/
		
		// 若要like查询，如下
		StringBuffer sql = new StringBuffer("select * from springboot_user where 1");
		if(name!=null&&!name.equals("")){
			// Sql.checkSql 的作用是防止sql注入
			sql.append(" and name like '%").append(Sql.checkSql(name)).append("%' ");
		}
		return queryForPage(sql.toString(), pageCurrent, pageSize, SpringBootUser.class);
	}

}
